package com.bing.index.writer;

import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;

import com.bing.util.AssertUtils;

public class IndexWriterFactory implements WriterFactory {
	
	private IndexWriterConfig indexWriterConfig;
	
	private FSDirectory fsDirectory;
	
	private IndexWriter indexWriter;
	
	public IndexWriterFactory(String index) {
		
		AssertUtils.notBlank(index, "index must not be null!");
		this.indexWriterConfig = new IndexWriterConfig(new WhitespaceAnalyzer());
		try {
			this.fsDirectory = FSDirectory.open(Paths.get(index));
			this.indexWriter = new IndexWriter(fsDirectory, indexWriterConfig);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public IndexWriterConfig getIndexWriterConfig() {
		return indexWriterConfig;
	}

	public void setIndexWriterConfig(IndexWriterConfig indexWriterConfig) {
		this.indexWriterConfig = indexWriterConfig;
	}
	
	public FSDirectory getFsDirectory() {
		return fsDirectory;
	}

	public void setFsDirectory(FSDirectory fsDirectory) {
		this.fsDirectory = fsDirectory;
	}

	@Override
	public IndexWriter getIndexWriter() {
		return this.indexWriter;
	}

}
